package org.dreamwork.jasmine2.sample.page;

import org.dreamwork.jasmine2.events.EventException;
import org.dreamwork.jasmine2.events.IClickListener;
import org.dreamwork.jasmine2.sample.data.Product;
import org.dreamwork.jasmine2.sample.util.Const;
import org.dreamwork.jasmine2.web.IWebControl;
import org.dreamwork.jasmine2.web.controls.*;
import org.dreamwork.util.IDataCollection;
import org.dreamwork.util.ListDataCollection;
import org.dreamwork.util.StringUtil;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: seth.yang
 * Date: 2010-10-25
 * Time: 16:25:01
 */
public class ProductListPage extends Page implements IClickListener {
    protected IDataCollection<Object> data;
    protected Product.Category[] categories = Product.Category.values ();

    protected Pagging2 pagging;
    protected Hidden ids;
    protected DropdownList category;
    protected TextBox productName;
    protected Button btnQuery;

    @Override
    public void onPagePreload (Page page) throws EventException {
        btnQuery.addClickListener (this);
    }

    @Override
    public void onPageLoadCompleted (Page page) throws EventException {
        if (!StringUtil.isEmpty (ids.getText ())) {
            HttpSession session = context.getSession ();
            List list = (List) session.getAttribute (Const.PRODUCT_LIST_DATA);
            if (list != null) {
                String[] a = ids.getText ().trim ().split (",");
                int[] i = new int[a.length];
                List<Product> ps = new ArrayList<Product> ();
                for (int n = 0; n < a.length; n ++) i[n] = Integer.parseInt (a[n]);
                for (Object o : list) {
                    Product p = (Product) o;
                    for (int id : i) {
                        if (p.getId () == id) ps.add (p);
                    }
                }
                for (Product p : ps) list.remove (p);
            }
        }

        try {
            data = query ();
            dataBind ();
        } catch (Throwable throwable) {
            throwable.printStackTrace ();
            throw new EventException (throwable);
        }
    }

    public void onClick (IWebControl sender) throws EventException {
        pagging.setPageNo (1);
    }

    /**
     * 模拟查询
     * @return 结果数据
     */
    private IDataCollection<Object> query () {
        HttpSession session = context.getSession ();
        List list = (List) session.getAttribute (Const.PRODUCT_LIST_DATA);
        if (list == null) return null;

        List<Object> copy = new ArrayList<Object> ();
        Product.Category c = (Product.Category) category.getSelectedItem ();
        String name = productName.getText ();
        for (Object o : list) {
            Product p = (Product) o;

            if (!StringUtil.isEmpty (name) &&
                    !p.getName ().toLowerCase ().contains (name.trim ().toLowerCase ()))
                continue;
            if (c != null && c != p.getCategory ())
                continue;
            copy.add (o);
        }
        if (copy.size () == 0) return null;

        int pageSize = pagging.getPageSize ();
        int pageNo = pagging.getPageNo ();
        ListDataCollection<Object> ldc = new ListDataCollection<Object> ();
        int start = (pageNo - 1) * pageSize;
        for (int i = start; i < copy.size () && i - start < pageSize; i++) {
            ldc.add (copy.get (i));
        }
        ldc.setPageNo (pageNo);
        ldc.setPageSize (pageSize);
        return ldc;
    }
}